package mx.itesm.twritter.server.ontology;

import java.util.*;
import java.io.*;
import java.util.Scanner;

public class Ontology {
	public ArrayList<ArrayList<String>> routes;
	public static final String FILE = "../src/mx/itesm/twritter/server/ontology/osmRoutes.csv";

	public Ontology(String ruta) {
		routes = readRoutes(ruta);
	}
	
	public Ontology() {
		routes = readRoutes(FILE);
	}

	private ArrayList<ArrayList<String>> readRoutes(String file) {
		
		Scanner src;
		String rt;
		ArrayList<ArrayList<String>> finalRoutes = new ArrayList<ArrayList<String>>();
		ArrayList<String> routes; 
		
		try {
			//src = new Scanner(new FileReader(file));

			src = new Scanner(new BufferedReader(new InputStreamReader(
								new FileInputStream(file), "UTF8")));
			while (src.hasNext()) {
				routes = new ArrayList<String>();
				rt = src.nextLine();
				String [] splitRoutes = rt.toLowerCase().split(",");
				for(String st : splitRoutes){
					if(!st.replaceAll(",", "").trim().isEmpty())
						routes.add(st.replaceAll(",", "").trim());
				}
				finalRoutes.add(routes);
			}

			src.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} 
		
		return finalRoutes;
	}
	
	public ArrayList<String> searchRoute(String route){
		
		String cleanRoute = route.toLowerCase().trim();
		for(ArrayList<String> arr : this.routes){
			for(String s : arr){
				if(cleanRoute.equals(s)){
					return arr;
				}
			}
		}
		ArrayList<String> arr = new ArrayList<String>();
		arr.add(route);
		return arr;
	}

}
